<template>
  <div class="flip-carousel">
    <el-button class="nav-btn left" @click="prev" circle>
      <el-icon>
        <ArrowLeft />
      </el-icon>
    </el-button>
    <div class="flip-track">
      <div v-for="(item, idx) in items" :key="idx" class="flip-card" :class="{ active: idx === current }"
        :style="cardStyle(idx)" @click="goTo(idx)">
        <div class="flip-inner" :class="{ flipped: idx === current && flipped }">
          <div class="flip-front">
            <img v-if="item.img" :src="item.img" :alt="item.title || ''" />
            <div v-else class="flip-content">
              <slot :item="item">{{ item.title }}</slot>
            </div>
          </div>
          <div class="flip-back">
            <div class="flip-desc">{{ item.desc || '更多描述内容' }}</div>
          </div>
        </div>
      </div>
    </div>
    <el-button class="nav-btn right" @click="next" circle>
      <el-icon>
        <ArrowRight />
      </el-icon>
    </el-button>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ArrowLeft, ArrowRight } from '@element-plus/icons-vue'

const props = defineProps({
  items: { type: Array, required: true },
  visible: { type: Number, default: 3 },
})

const current = ref(0)
const flipped = ref(false)
const len = props.items.length

const prev = () => {
  flipped.value = false
  setTimeout(() => {
    current.value = (current.value - 1 + len) % len
  }, 300)
}
const next = () => {
  flipped.value = false
  setTimeout(() => {
    current.value = (current.value + 1) % len
  }, 300)
}
const goTo = (idx) => {
  if (idx === current.value) {
    flipped.value = !flipped.value
  } else {
    flipped.value = false
    setTimeout(() => {
      current.value = idx
    }, 300)
  }
}

const cardStyle = (idx) => {
  const mid = Math.floor(props.visible / 2)
  let offset = (idx - current.value + len) % len
  if (offset > len / 2) offset -= len
  if (Math.abs(offset) > mid) return { opacity: 0, pointerEvents: 'none' }
  return {
    zIndex: props.visible - Math.abs(offset),
    opacity: 1 - Math.abs(offset) * 0.18,
    transform: `translateX(${offset * 120}px) scale(${1 - Math.abs(offset) * 0.12})`,
    transition: 'all 0.5s cubic-bezier(.34,1.56,.64,1)',
    cursor: offset === 0 ? 'pointer' : 'default',
  }
}
</script>

<style lang="scss" scoped>
.flip-carousel {
  position: relative;
  width: 600px;
  height: 320px;
  margin: 0 auto;
  display: flex;
  align-items: center;

  .flip-track {
    position: relative;
    width: 100%;
    height: 260px;
    display: flex;
    align-items: center;
    justify-content: center;
    perspective: 1200px;
  }

  .flip-card {
    position: absolute;
    left: 50%;
    top: 0;
    width: 220px;
    height: 260px;
    border-radius: 18px;
    overflow: visible;
    background: transparent;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;

    .flip-inner {
      width: 100%;
      height: 100%;
      transition: transform 0.6s cubic-bezier(.34, 1.56, .64, 1);
      transform-style: preserve-3d;
      position: relative;

      &.flipped {
        transform: rotateY(180deg);
      }
    }

    .flip-front,
    .flip-back {
      position: absolute;
      width: 100%;
      height: 100%;
      backface-visibility: hidden;
      border-radius: 18px;
      overflow: hidden;
      background: #fff;
      box-shadow: 0 4px 24px #e0e7ff;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .flip-front {
      z-index: 2;

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        border-radius: 18px;
      }

      .flip-content {
        font-size: 1.2rem;
        color: #333;
        text-align: center;
      }
    }

    .flip-back {
      transform: rotateY(180deg);
      background: #6366f1;
      color: #fff;

      .flip-desc {
        padding: 24px;
        font-size: 1.1rem;
        text-align: center;
      }
    }

    &.active {
      box-shadow: 0 8px 40px #6366f1;
    }
  }

  .nav-btn {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 10;

    &.left {
      left: -48px;
    }

    &.right {
      right: -48px;
    }
  }
}
</style>